package com.yf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.dto.Page;
import com.yf.entity.Student;
import com.yf.mapper.StudentMapper;
import com.yf.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public void addStudent(Student student) {
        studentMapper.addStudent(student);
    }

    @Override
    public void deleteStudent(Integer studentId) {
        studentMapper.deleteStudent(studentId);
    }

    @Override
    public void updateStudent(Student student) {
        studentMapper.updateStudent(student);
    }

    @Override
    public Student getStudentById(Integer studentId) {
        return studentMapper.getStudentById(studentId);
    }

    @Override
    public Page getStudentPage(Integer pageNo, Integer pageSize) {
        Page page = new Page();

        PageHelper.startPage(pageNo,pageSize);
        List<Student> studentList = studentMapper.getStudentList();
        PageInfo<Student> info = new PageInfo<>(studentList);

        page.setPageNo(info.getPageNum());
        page.setPageSize(info.getPageSize());
        page.setHasPre(info.isHasPreviousPage());
        page.setHasNext(info.isHasNextPage());
        page.setPageCount(info.getPages());
        page.setCount((int) info.getTotal());
        page.setData(info.getList());

        return page;
    }
}
